package edu.codink.xq.center.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import edu.codink.common.base.BaseController;
import edu.codink.xq.center.dao.MenuConfig;
import edu.codink.xq.center.dao.UserDao;
import edu.codink.xq.center.dto.UserMenu;
import edu.codink.xq.center.entity.Userinfo;

/**
 * 登录
 * 
 * @author ylsu
 * 
 */
@Controller
public class UserManagerController extends BaseController
{
	@Autowired
	private UserDao userDao;

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(String username, String password,
			HttpServletRequest request)
	{
		List<Userinfo> users = this.userDao.getLoginUser(username, password);
		if (users.size() == 0)
		{
			return new ModelAndView("loginFail.jsp");
		}
		Userinfo user = users.get(0);
		HashMap<String,UserMenu> menuMap = MenuConfig.getMenuConfigMap();
		String codeStr = "";
		if ("admin".equals(user.getLoginname())) 
		{
			for(String key:menuMap.keySet())
			{
				codeStr += key + ",";
			}
		}
		else
		{
			for(String key:menuMap.keySet())
			{
				if ("sys".equals(key))
				{
					continue;
				}
				codeStr += key + ",";
			}
		}
		
		
		request.setAttribute("codes",codeStr);
		setLoginUser(request, user);
		return new ModelAndView("main.jsp");
	}
}
